class TypeUtil {
  static t = {
    s: 1000,
    min: 1000 * 60,
    hour: 1000 * 60 * 60,
    day: 1000 * 60 * 60 * 24,
  };
  public static isEmpytObject(obj){
    if(obj instanceof Object){
      if(Object.keys(obj).length==0) return true;
      return false;
    }
    return true;
  }
  public static copy = (target: object, from: object) => {
    if (target instanceof Object && from instanceof Object)
      for (let key in from) {
        target[key] = from[key];
      }
  };
  public static commonKeyCopy(target:object,from:object){
    if (target instanceof Object && from instanceof Object)
      for (let key in target) { 
          target[key] = from[key];   
  };
}
  public static anyCopy(k: string[], from: object) {
    let t = {};
    for (let key of k) {
      t[key] = from[key];
    }
    return t;
  }
  static deepCopy<T>(obj: object): T {
    let val: any;
    if (obj instanceof Object) {
      if (obj instanceof Array) {
        val = [];
        for (let key in obj) {
          val[key] = this.deepCopy(obj[key]);
        }
      } else {
        val = {};
        for (let key in obj) {
          val[key] = this.deepCopy(obj[key]);
        }
      }
    } else val = obj;

    return val;
  }
  static uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
      s[i] = hexDigits.substring(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substring((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
  }
  static mapToArray(m: Map<any, any>) {
    let arr = [];
    m.forEach((v, k) => {
      arr.push(k);
    });
    return arr;
  }
  static timeConverse(time: string): string {
    if (!time) return "";
    let date = new Date(time);
    let nowTime = new Date();
    let space = nowTime.getTime() - date.getTime();
    let showStr = "";
    if (space < this.t.min) showStr = "刚刚";
    else if (space < this.t.hour)
      showStr = (space / this.t.min).toFixed(0) + "分钟前";
    else if (space < this.t.day)
      showStr = (space / this.t.hour).toFixed(0) + "小时前";
    else if (space < this.t.day * 30)
      showStr = (space / this.t.day).toFixed(0) + "天前";
    else
      showStr =
        date.getFullYear() + "/" + date.getMonth() + "/" + date.getDay();
    return showStr;
  }
  static toString(obj: any) {
    if (typeof obj == "string") return obj;
    else return JSON.stringify(obj);
  }
}

export default TypeUtil;
